import time

from ...core.output.output_go import OutGo

from CoTec.utility.string.string_go import genUUID
from CoTec.utility.date.date_go import DateGo

class Annoations:

    @staticmethod
    def exe_time(func: object) -> object:
        def new_func(*args, **args2):
            t0 = time.time()
            # print("@%s, {%s} start" % (time.strftime("%X", time.localtime()), func.__name__))
            back = func(*args, **args2)
            # print("@%s, {%s} end" % (time.strftime("%X", time.localtime()), func.__name__))

            # print("---------------------------------------------------------")
            # print("@%.3fs taken for {%s}" % (time.time() - t0, func.__name__))
            # print("---------------------------------------------------------")

            OutGo.pr("@%.3fs taken for {%s}" % (time.time() - t0, func.__name__))

            return back

        return new_func

    @staticmethod
    def mr(type, struc, dao):
        """
        任务记录
        :param type:
        :param struc:
        :param dao:
        :return:
        """
        id = genUUID()

        def decorator(func):
            def mr(*args, **kw):
                save_dic = {
                    struc.begin.key: DateGo.get_current_date(),
                    struc.status.key: '1',
                    struc.type.key: type}
                sta = dao.save(id, save_dic)

                count = func(*args, **kw)

                if count == -1:
                    status = '-1'
                else:
                    status = '0'

                update_dic = {
                    struc.end.key: DateGo.get_current_date(),
                    struc.status.key: status,
                    struc.count.key:count}
                sta = dao.update(update_dic, {'id':id})

                # print("函数%s耗时%d" % (func.__name__, end = begin))
                return count

            return mr

        return decorator

    @staticmethod
    def mission_report(type, struc, dao):
        """
        任务记录
        :param type:
        :param struc:
        :param dao:
        :return:
        """
        id = genUUID()

        def decorator(func):
            def mr(*args, **kw):
                save_dic = {
                    struc.begin.key: DateGo.get_current_date(),
                    struc.status.key: '1',
                    struc.type.key: type}
                sta = dao.save(id, save_dic)

                count = func(*args, **kw)

                if count == -1:
                    status = '-1'
                else:
                    status = '0'

                update_dic = {
                    struc.end.key: DateGo.get_current_date(),
                    struc.status.key: status,
                    struc.count.key:count}
                sta = dao.update(update_dic, {'id':id})

                # print("函数%s耗时%d" % (func.__name__, end = begin))
            return mr

        return decorator